14. Exercise: Add Snooze Action

L1 A14 Add Snooze Action

In this lesson, you are already given a BroadcastReceiver named SnoozeReceiver. You will use SnoozeReceiver to receive the user click on the Notification action. In the following steps you will add code to snooze the egg timer notification for 60 seconds when the user clicks the snooze action button. When the snooze action is clicked, the SnoozeReceiver will receive an intent and will create a new alarm to send a new notification after 60 seconds.

Exercise

  1. Open SnoozeReceiver.kt. This class is similar to AlarmReceiver which we used before. In the following steps we will add code which will trigger onReceive function of the SnoozeReceiver.



  2. SnoozeReceiver can snooze a notification but it also needs to remove the snoozed notification. Scrolldown to bottom of the onReceive function, get an instance of notificationManager from system and call cancelAll.
        val notificationManager = ContextCompat.getSystemService(
            context,
            NotificationManager::class.java
        ) as NotificationManager
        notificationManager.cancelAll()

With this SnoozeReceiver will not only schedule a new notification but will also remove the snoozed one.



  1. In order to use SnoozeReceiver, go back to NotificationUtils.kt and create a new intent just after the style in sendNotification() function. You need to create a PendingIntent which will be used by the system to set up a new alarm to post a new notification after 60 secs when the snooze button is tapped by the user. To create a pending intent, call getBroadcast() method on PendingIntent which expects….
    • the application context in which this PendingIntent should start the activity.
    • the request code, which is the request code for this pending intent. If you need to update or cancel this pending intent, you need to use this code to access the pending intent.
    • the snoozeIntent object which is the intent of the activity to be launched
    • the flag value of #FLAG_ONE_SHOTsince the intent will be used only once. The quick action and the notification will disappear after the first tap which is why the intent can only be used once.
// NotificationUtils.kt

// TODO: Step 2.2 add snooze action
val snoozeIntent = Intent(applicationContext, SnoozeReceiver::class.java)
val snoozePendingIntent: PendingIntent = PendingIntent.getBroadcast(
    applicationContext, 
    REQUEST_CODE, 
    snoozeIntent, 
    FLAGS
)
  1. Next, call the addAction() function on the notificationBuilder. This function expects an icon and a text to describe your action to the user. You also need to add the snoozeIntent to notificationBuilder. This intent will be used to trigger the right broadcastReceiver when your action is clicked.
// TODO: Step 2.3 add snooze action
.addAction(
    R.drawable.egg_icon, 
    applicationContext.getString(R.string.snooze),
    snoozePendingIntent
)
  1. Now run the egg timer again to test the snooze action. Run the timer and put the app in the background. Once the timer is up, expand the notification and you will see the notification now has a snooze action button which snoozes the egg timer for another minute.